package com.leo.foodmaster.admin.foodMaterial.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.foodMaterial.entity.FoodMaterialType;
import com.leo.foodmaster.admin.foodMaterial.service.IFoodMaterialTypeService;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  食材类型前端控制器
 * </p>
 *
 * @author Leo
 * @since 2021-07-14
 */
@RestController
@AllArgsConstructor
@Api(tags = "食材类型管理")
@RequestMapping("/foodMaterial/type")
public class FoodMaterialTypeController {
    private final IFoodMaterialTypeService foodMaterialTypeService;

    @ApiOperation("条件分页查询食材类型列表")
    @GetMapping("/list")
    public Result<IPage<FoodMaterialType>> getFoodMaterialTypeList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "食材类型名称") @RequestParam(required = false) String name) {
        IPage<FoodMaterialType> pageList  = foodMaterialTypeService
                .getFoodMaterialTypePageList(new Page<>(pageNum, pageSize), name);
        return Result.success(pageList);
    }

    @ApiOperation("查询公司所有食材类型列表")
    @GetMapping("/listAll")
    public Result<List<FoodMaterialType>> getAllFoodMaterialTypeList( ) {
        Long organId = WebUtils.getUser().getOrganId();
        List<FoodMaterialType> list  = foodMaterialTypeService
                .list(Wrappers.lambdaQuery(FoodMaterialType.class)
                .eq(FoodMaterialType::getOrganId, organId));
        return Result.success(list);
    }

    @PostMapping
    @ApiOperation("新增食材类型")
    public Result<Boolean> addFoodMaterialType(@RequestBody FoodMaterialType foodMaterialType) {
        boolean save = foodMaterialTypeService.add(foodMaterialType);
        return Result.success(save);
    }

    @PutMapping
    @ApiOperation("修改食材类型")
    public Result<Boolean> updateFoodMaterialType(@RequestBody FoodMaterialType foodMaterialType) {
        boolean update = foodMaterialTypeService.updateById(foodMaterialType);
        return Result.success(update);
    }

    @DeleteMapping("/{foodMaterialTypeIds}")
    @ApiOperation("删除食材类型")
    public Result<Object> deleteFoodMaterialType(@ApiParam("食材类型ids") @PathVariable String foodMaterialTypeIds) {
        return foodMaterialTypeService.deleteFoodMaterialType(foodMaterialTypeIds);
    }
}

